package org.spring.springboot.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 城市 Controller 实现 Restful HTTP 服务
 * <p>
 * Created by bysocket on 07/02/2017.
 */
@Controller
@RequestMapping("/test1")
public class CityController {
	private static Logger logger = LoggerFactory.getLogger(CityController.class);
    @Autowired
    private CityService cityService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model,HttpServletRequest req){
    	model.addAttribute("city", "1212");
    	HttpSession session = req.getSession();
    	List<String> list=new ArrayList<String>();
    	list.add("111");
    	list.add("222");
    	model.addAttribute("session",list);
    	
    	 // 记录debug级别的信息
        logger.debug("This is debug message.");
        // 记录info级别的信息
        logger.info("This is info message.");
        // 记录error级别的信息
        logger.error("This is error message.");
        return "city";
    }
    
    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
    public String findOneCity(Model model, @PathVariable("id") Long id) {
    	
    	
        //model.addAttribute("city", cityService.findCityById(id));
    	City city = new City();
    	city.setId(1L);
    	city.setCityName("beijing");
    	model.addAttribute(city);
    	
        return "city";
    }

    @RequestMapping(value = "/api/city", method = RequestMethod.GET)
    public String findAllCity(Model model) {
       // List<City> cityList = cityService.findAllCity();
    	List<City> cityList = new ArrayList<City>();
    	
        City city1 = new City();
    	city1.setId(1L);
    	city1.setCityName("beijing");
    	
    	City city2 = new City();
    	 city2.setId(2L);
    	 city2.setCityName("sahngahi");
    	 
    	 cityList.add(city1);
    	 cityList.add(city2);
    	 
    	
        model.addAttribute("cityList",cityList);
        return "cityList";
    }
}
